<?php
/**
 * Short Description  
 *
 * Long Description 
 *
 * @copyright 
 * @version   $Id: Module.php 12 2011-04-09 08:58:51Z sandeep.manne@gmail.com $
 */

class Core_Module
{
	private $_moduleName = null;
	
	/* @var $_module Core_Module_Interface */
	private $_module     = null;
	
	public function __construct($moduleName)
	{
		$this->_module = App::getModule($this->_moduleName);
	}
	
	/**
	 * Used to install module
	 */
	public function install()
	{
		//@todo add validations before installation
		$this->_module->install();
	}
	
	/**
	 * Used to check dependency of module on php extensions
	 * @throws Exception
	 */
	public function checkDependency()
	{
		$requireExtensions = $this->_module->getDependency();
        foreach ( $requiredExtensions as $value ) {
            if ( !extension_loaded($value) ) {
                throw new Exception("Required PHP extension '{$value}' was not loaded.");
            }
        }
	}
	
	/**
	 * Return all details of module
	 */
	public function getDetails()
	{
		return $this->_module->getDetails();
	}
	
	
	/**
	 * Returns all available modules in the whole application
	 */
	public static function getAvailableModules()
	{
		$modules = array();
		$directories = scandir("../application/modules");
		foreach ($directories as $directory) {
			if(substr($directory,0,1) != '.') {
				$modules[] = $directory;
			}
		}
		return $modules;
	}
	
	/**
	 * Get all controllers in the given module
	 * 
	 * @param string $moduleName
	 */
	public static function getControllers($moduleName)
	{
		$controllers = array();
		$directories = scandir("../application/modules/".$moduleName."/controllers");
		foreach ($directories as $directory) {
			if(substr($directory,0,1) != '.') {
				 $controllerFileName = explode('Controller.php', $directory);
				 $controllers[]  =  $controllerFileName[0];
			}
		}
		return $controllers;
	}
	
	/**
	 * Get all actions available in given controller
	 * 
	 * @param string $moduleName
	 * @param string $controllerName
	 */
	public static function getActions($moduleName, $controllerName)
	{
		$actions = array();
		require_once $moduleName."/controllers/".ucfirst($controllerName)."Controller.php";
		echo ucfirst($moduleName)."_".ucfirst($controllerName)."Controller.php";
		$methods = get_class_methods(ucfirst($moduleName)."_".ucfirst($controllerName)."Controller");
		foreach ($methods as $method) {
			if (substr($method, -6,6) == "Action") {
				$actions[] = substr($method, 0, -6);
			}
		}
		return $actions;
	}
}